package org.kzwl.book.controller.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.kzwl.comm.advice.Consts;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.book.BooksBasicBO;
import org.kzwl.comm.pojo.dto.book.MyBooksDTO;
import org.kzwl.comm.pojo.dto.book.MyBooksItem;
import org.kzwl.service.app.BookUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webBookUser")
@Api("PC用户图书")
public class WebBookUserController {

    @Autowired
    private BookUserService bookUserService;

    @ApiOperation(value = "收藏", notes = "cuBookId(书籍id)--必填 cuUserId(用户id)--必填")
    @PostMapping("/addMyBooks")
    public ResponseData<Void> addMyBooks(@RequestBody BooksBasicBO bo) {
        bookUserService.addMyBooks(bo);
        return new ResponseData<>();
    }

    @ApiOperation(value = "取消收藏", notes = "cuBookId(书籍id)--必填 cuUserId(用户id)--必填")
    @PostMapping("/removeMyBooks")
    public ResponseData<Void> removeMyBooks(@RequestBody BooksBasicBO bo) {
        bookUserService.removeMyBooks(bo);
        return new ResponseData<>();
    }

    @ApiOperation(value = "我的书架", notes = "cuUserId(用户id)--必填 current(当前页数)--必填 size(一页几条)--选填")
    @PostMapping("/myBooks")
    public ResponseData<IPage<MyBooksItem>> myBooks(@RequestBody BooksBasicBO bo) {
        bo.setCuBookUserIsShow(1);
        IPage<MyBooksItem> dto = bookUserService.myBooksByWeb(bo);
        return new ResponseData<>(dto);
    }

    @ApiOperation(value = "更新阅读页数", notes = "cuBookId(书籍id)--必填 cuUserId(用户ID)--必填 cuBookUserContinue(阅读页数)--必填")
    @PostMapping("/updateContinue")
    public ResponseData<Void> updateContinue(@RequestBody BooksBasicBO bo) {
        bookUserService.updateContinue(bo);
        return new ResponseData<>();
    }

    @ApiOperation(value = "上次阅读的书籍", notes = "cuUserId(用户id)--必填")
    @PostMapping("/getPrevious")
    public ResponseData<MyBooksItem> getPrevious(@RequestBody BooksBasicBO bo) {
        MyBooksDTO dto = bookUserService.getPrevious(bo);
        if (dto.getMyBooksItem()!=null){
            dto.getMyBooksItem().setCuBookAddress(Consts.MyConsts.URLPATH + dto.getMyBooksItem().getCuBookAddress());
        }
        return new ResponseData<>(dto.getMyBooksItem());
    }

    @ApiOperation(value = "浏览记录", notes = "cuUserId(用户ID)--必填 current(当前页数)--必填 size(一页几条)--选填")
    @PostMapping("/getBrowsing")
    public ResponseData<IPage<MyBooksItem>> getBrowsing(@RequestBody BooksBasicBO bo) {
        IPage<MyBooksItem> page = bookUserService.myBooksByWeb(bo);
        return new ResponseData<>(page);
    }
}
